<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="java.util.*, dto.*, util.*, dao.FetchQuestions"%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Discussion</title>
        <script type="text/javascript" src="JavaScript/Javascripts.js"></script>
        <script type="text/javascript">
            function reg_user()
            {
                var ask = "<%=session.getAttribute("ask")%>";
                if(ask == 'Error') {
                    alert("An error occurred, please try after sometime");
            <%session.setAttribute("ask", null);%>
                            } else if (ask == "Success") {
                                alert("Your question has been submitted.\nPlease wait for a faculty to review it.");
            <%session.setAttribute("ask", null);%>
                            }
                            noBack();
                        }
        </script>
        <link href="CSS/Style.css" rel="stylesheet" type="text/css" />
    </head>
    <body onload="reg_user();" onpageshow="if (event.persisted) noBack();" onunload="">
        <jsp:include page="header.jsp"/>
        <div id="menu_wrapper">
            <div id="menu">
                <ul>
                    <li><a href="home.jsp" target="_parent">Home</a></li>
                    <li><a href="about.jsp" target="_parent">About Us</a></li>
                    <li><a href="discuss.jsp" class="current">Discussion</a></li>
                    <%
                                if (session.getAttribute("user") == null) {
                    %>
                    <li><a href="Register.jsp" target="_parent">Register</a></li>
                    <%} else {%>
                    <li><a href="Control.jsp" target="_parent">User CP</a></li>
                    <%}%>
                </ul>
            </div> <!-- end of menu -->
        </div><!-- end of menu_wrapper -->
        <div id="content_wrapper">
            <div id="sidebar">
                <jsp:include page="login.jsp"/>
                <jsp:include page="subjects.jsp"/>
            </div> <!-- end of sidebar -->
            <div id="content">
                <div class="content_box">
                    <h2 align="center">Welcome to Discuss Page</h2>
                    <%  LoggedUser loggedUser = (LoggedUser) session.getAttribute("user");
                                if (loggedUser == null) {
                    %>                
                    <h3 align="center">You are not authorized to view this page.<br/>
                        Please login to get access or <a href="Register.jsp">regiser</a>.</h3>
                        <%} else {
                        boolean isAdmin = Constants.Admin.equalsIgnoreCase(loggedUser.getType());
                        List<String> subjects = loggedUser.getSubjects();
                        String subject = request.getParameter("subject");
                        if(subject != null) {
                            subject = subject.replaceAll("~AND~", "&");
                        }
                        String status = request.getParameter("status");
                        String quesId = request.getParameter("q");
                        session.setAttribute("subject", subject);
                        session.setAttribute("quesId", quesId);
                        if (subjects == null || subjects.isEmpty()) {%>
                    <h3 align="center">Your subjects are not yet registered.</h3>
                    <%} else if (subject != null && !subjects.contains(subject)) {
                        if(isAdmin) { %>
                    <h3 align="center">Invalid subject name specified.</h3>
                        <%} else {%>
                        <h3 align="center">You are not authorized to view questions of <%=subject%>.<br/>
                        Please contact Administrator to report problem.</h3>
                        <%}} else if (subject == null && (status == null || quesId == null)) {%>
                    <center>
                        <form name="quesForm" action="./AskQuestion" method="post" onsubmit="return ques_frm()">
                            <h3>Please choose an option from sidebar.
                                <% if(!isAdmin) {%>
                                <br/>or<br/>Ask a question:
                                <select name="subject">
                                    <option>Select</option>
                                    <% for (int i = 0; i < subjects.size(); i++) {%>
                                    <option><%=subjects.get(i)%></option>
                                    <%}%>
                                </select>
                            </h3>
                            <textarea name="text" rows="5" cols="20" style="width: 400px;height: 150px;max-width: 400px;max-height: 150px"></textarea>
                            <br/>
                            <input type="submit" value="Ask Question"/>
                            <%}%>
                        </form>
                    </center>
                    <% } else if (subject != null && status != null && quesId == null) {
                    List<Question> list = FetchQuestions.viewQuestions(status, subject, loggedUser.getUserId(), loggedUser.getType());
                    if (!list.isEmpty()) {%>
                    <table border="1" align="center" style="border-color: #008400;text-align: center" width="520px">
                        <tr>
                            <th width="60%">Question</th>
                            <th width="20%">Asked By</th>
                            <th width="15%">Status</th>
                            <th width="10%">Replies</th></tr>
                    <%for(int i= 0; i<list.size();i++) {
                        Question q = list.get(i);
                        String text = q.getText().length()>50?q.getText().substring(0, 47).concat("..."):q.getText();%>
                        <tr><td style="text-align: left"><a href="discuss.jsp?subject=<%=subject%>&q=<%=q.getQuesId()%>"><%=text%></a></td><td><%=q.getAskedBy()%></td><td><%=status%></td><td><%=q.getReplies()%></td></tr>
                        <%}%>
                    </table>
                    <%} else {%>
                    No question found.
                    <%}} else if (subject != null && status == null && quesId != null) {
                        Question ques = new Question();
                        List<Answer> ansList = new ArrayList<Answer>();
                        FetchQuestions.viewQuestion(subject, quesId, loggedUser, ques, ansList);
                        if(ques.getQuesId() == null) {%>
                        <h3 align="center">No question found for this Subject/Question combination.<br/>
                            Please contact Administrator to report problem.</h3>
                        <%}
                        else if ("-1".equalsIgnoreCase(ques.getQuesId())){%>
                        <h3 align="center">You are not authorized to view this question.<br/>
                            Please contact Administrator to report problem.</h3>
                        <%} else {%>
                            <table border="1" style="border-color: #008400;text-align: left" width="520px">
                                <tr><td colspan="2" align="left">Question:</td></tr>
                                <tr>
                                    <td width="30%">Asked By: <%=ques.getAskedBy()%><br/><%=ques.getTimestamp()%></td>
                                    <td width="70%" charoff=" "><%=ques.getText()%></td>
                                </tr>
                                <% if(Constants.Rejected.equalsIgnoreCase(ques.getStatus())) {%>
                                <tr><td colspan="2"><%=ques.getStatus()%> By: <%=ques.getReviewedBy()%> (<%=ansList.get(0).getTimeStamp()%>)</td></tr>
                                <tr><td colspan="2">Rejecting Reason:<br/><br/><%=ansList.get(0).getAnsText()%></td></tr>
                                <%} else if(Constants.Open.equalsIgnoreCase(ques.getStatus())) {
                                    String userType = loggedUser.getType();
                                    if(Constants.Student.equalsIgnoreCase(userType)) {%>
                                    <tr><td colspan="2">Please wait for a faculty to review it.</td></tr>
                                    <%} else if (Constants.Faculty.equalsIgnoreCase(userType)) {%>
                                    <form name="approval" action="./Approval" method="post" onsubmit="return aprv_Form()">
                                        <tr><td colspan="2">
                                                <textarea name="ans" rows="5" cols="20" style="width: 400px;height: 150px;max-width: 400px;max-height: 150px"></textarea>
                                            </td>
                                        </tr>
                                        <tr><td>
                                                <input type="radio" name="review" value="Approved" checked="checked"/>Approve
                                                <br/><input type="radio" name="review" value="Rejected"/>Reject
                                            </td>
                                            <td>
                                                <input type="submit" value="Submit" alt="Submit"/>
                                            </td>
                                        </tr>
                                    </form>
                                    <%}} else {%>
                                    <tr><td colspan="2"><%=ques.getStatus()%> By: <%=ques.getReviewedBy()%> (<%=ansList.get(0).getTimeStamp()%>)</td></tr>
                                    <% for(int i = 0;i<ansList.size();i++) {
                                        Answer ans = ansList.get(i); %>
                                        <tr><td width="30%">Answered By: <%=ans.getAnsBy() %><br/><%=ans.getTimeStamp()%></td>
                                            <td width="70%" charoff=" "><%=ans.getAnsText()%></td></tr>
                                    <%}
                                    if(!isAdmin){%>
                                    <form name="answer" action="./SubmitAnswer" method="post" onsubmit="return ans_form()">
                                    <tr><td colspan="2">Add a new answer:</td></tr>
                                    <tr><td colspan="2">
                                            <textarea name="ans" rows="5" cols="20" style="width: 400px;height: 150px;max-width: 400px;max-height: 150px"></textarea>
                                        </td>
                                    </tr>
                                    <tr><td>
                                            <input type="submit" value="Submit Answer" alt="Submit Answer"/>
                                        </td>
                                        <td>
                                            <input type="reset" value="Clear" alt="Clear"/>
                                        </td>
                                    </tr>
                                    </form>
                                <%}}%>
                            </table>
                        <%}
                    }}%>

                    <div class="cleaner"></div>
                </div>

                <div class="content_box_bottom"></div>

            </div> <!-- end of content -->

            <div class="cleaner"></div>

        </div>

        <jsp:include page="footer.jsp"/>
    </body>
</html>